/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.hnilice.excel;

import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 *
 * @author swosh
 */
public class Main {

    private final static File excel = new File("");

    /**
     * @param arg[0] absolute or relative patch to file
     *        arg[1] param for action: s is save, u is update,
     *        us is for update and save
     *        If is not specified so default is save
     */
    public static void main(String[] args) {
        Parser parser = new Parser();
        TerminDAO terminDAO = new TerminDAO();
        File file = new File(args[0]);
        TreeSet<TerminCisOri> treeSetTerminCO = (TreeSet<TerminCisOri>) parser.parseForSave(file);

        if (args[1].equalsIgnoreCase("s") || args[1].equalsIgnoreCase("")) {
            for (Iterator<TerminCisOri> it = treeSetTerminCO.iterator(); it.hasNext();) {
                TerminCisOri termin = it.next();
                int[] cisOri2Save = termin.getCisOrientacni();
                boolean isFound = false;
                boolean isOk2Save = true;
                Integer[] cisOri2SIO;
                if (cisOri2Save != null) {
                    cisOri2SIO = new Integer[cisOri2Save.length];


                    for (int i = 0; i < cisOri2Save.length; i++) {
                        cisOri2SIO[i] = new Integer(cisOri2Save[i]);
                    }
                    Set<Integer> cisOri2SHS = new HashSet(Arrays.asList(cisOri2SIO));
                    Set<TerminCisOri> terminsForCheck =
                            (Set<TerminCisOri>) terminDAO.selectTerminsByNUliceNAkceTCis(termin.getNazUlice(), termin.getTerCis(), termin.getNazAkce());
                    for (Iterator<TerminCisOri> it1 = terminsForCheck.iterator(); it1.hasNext();) {
                        TerminCisOri terminCisOrientacni = it1.next();
                        int[] cisOri2Check;
                        if (terminCisOrientacni.getCisOrientacni() != null){
                        cisOri2Check = terminCisOrientacni.getCisOrientacni();
                        } else {
                        cisOri2Check = new int[0];
                        }
                        for (int i = 0; i < cisOri2Check.length; i++) {
                            int j = cisOri2Check[i];

                            isFound = cisOri2SHS.contains(new Integer(j));
                            if (isFound) {
                                isOk2Save = false;
                                break;
                            }
                        }

                    }
                    if (isOk2Save) {
                        terminDAO.saveTermin(termin);
                    } else {
                        //TODO 'Pro stejnou akci, ve stejný den, ve stejné ulici nemůže být ani jedno číslo orientační stejné. Jedná se o duplikaci. '
                        //'Použij jiný název akce';
                    }
                }
            }
        } else if (args[1].equalsIgnoreCase("u")) {
            for (Iterator<TerminCisOri> it = treeSetTerminCO.iterator(); it.hasNext();) {
                TerminCisOri termin = it.next();
                //terminDAO.updateTermin(termin);
            }
        }





    }

}
